package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.entity.MerchantPermission;
import com.ayf.payment.game.api.entity.MerchantRole;
import com.ayf.payment.game.api.entity.MerchantRolePermission;
import com.ayf.payment.game.api.entity.MerchantSon;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantPermissionMapper;
import com.ayf.payment.game.api.mapper.MerchantRoleMapper;
import com.ayf.payment.game.api.mapper.MerchantRolePermissionMapper;
import com.ayf.payment.game.api.mapper.MerchantSonMapper;
import com.ayf.payment.game.api.service.MerchantRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MerchantRoleServiceImpl implements MerchantRoleService {

    @Resource
    private MerchantRoleMapper merchantRoleMapper;
    @Resource
    private MerchantPermissionMapper merchantPermissionMapper;
    @Resource
    private MerchantRolePermissionMapper merchantRolePermissionMapper;
    @Resource
    private MerchantSonMapper merchantSonMapper;

    @Override
    public int addMerchantAdmin(MerchantRole merchantRole) {
        merchantRole.setName("超级管理员");
        merchantRole.setRoleStatus(1);
        merchantRole.setRemark("包含所有权限");
        merchantRoleMapper.insertSelective(merchantRole);
        List<MerchantPermission> permissionList = merchantPermissionMapper.selectAll();
//        permissionList.s
//        merchantRolePermissionMapper.addMerchantRolePermission(merchantRole.getId(), permissionList);
        return merchantRole.getId();
    }

    @Override
    public List<MerchantRole> list(MerchantRole merchantRole) {
        return merchantRoleMapper.select(merchantRole);
    }

    @Override
    public boolean addRole(MerchantRole merchantRole) {
        return merchantRoleMapper.insertSelective(merchantRole) > 0;
    }

    @Override
    public boolean deleteRole(MerchantRole merchantRole) {
        if (merchantRole.getId() == null) {
            throw new ParameterException("角色id不能为空");
        }
        MerchantSon merchantSon = new MerchantSon();
        merchantSon.setRoleId(merchantRole.getId());
        List<MerchantSon> sons = merchantSonMapper.select(merchantSon);
        if (!CollectionUtils.isEmpty(sons)) {
            throw new ParameterException("该角色有绑定的子账号，不能删除");
        }
        MerchantRole role = merchantRoleMapper.selectByPrimaryKey(merchantRole.getId());
        if (StringUtils.isNotBlank(role.getCode()) && role.getCode().equals("TuiG")) {
            throw new ParameterException("推广员角色不能删除");
        }
        MerchantRolePermission merchantRolePermission = new MerchantRolePermission();
        merchantRolePermission.setRoleId(merchantRole.getId());
        merchantRolePermissionMapper.delete(merchantRolePermission);
        return merchantRoleMapper.deleteByPrimaryKey(merchantRole.getId()) > 0;
    }

    @Override
    public boolean updateRole(MerchantRole merchantRole) {
        if (merchantRole.getId() == null) {
            throw new ParameterException("角色id不能为空");
        }
        return merchantRoleMapper.updateByPrimaryKeySelective(merchantRole) > 0;
    }
}
